package leetcode_1501_1600;

public class LeeCode_1574 {
    public static void main(String[] args) {
//        System.out.println(findLengthOfShortestSubarray(new int[]{1, 2, 3, 10, 4, 2, 3, 5}));
//        System.out.println(findLengthOfShortestSubarray(new int[]{5,4,3,2,1}));
//        System.out.println(findLengthOfShortestSubarray(new int[]{1,2,3}));
//        System.out.println(findLengthOfShortestSubarray(new int[]{1}));
        System.out.println(findLengthOfShortestSubarray(new int[]{16,10,0,3,22,1,14,7,1,12,15}));
    }
    private static int findLengthOfShortestSubarray(int[] arr) {
        int l = arr.length, s = 0, e = -1, r = l;
        for (int i = 0; i < l - 1; i++) {
            if (arr[i] > arr[i + 1]){
                if (s == 0){
                    s = i + 1;
                    e = s;
                }else
                    e = i;
            }
        }
        if (s > e)
            return 0;
        for (int i = s - 1; i >= 0; i--) {
            int index = l - 1;
            for (int j = e + 1; j < l; j++) {
                if (arr[j] >= arr[i]){
                    index = j - 1;
                    break;
                }
            }
            r = Math.min(r, index - i);
        }
        r = Math.min(r, e + 1);
        return r;
    }
}
